# 整理今天笔记，课上代码最少敲3遍。
# 用列表推导式做下列小题
# 过滤掉长度小于3的字符串列表，并将剩下的转换成大写字母
# 求(x,y)其中x是0-5之间的偶数，y是0-5之间的奇数组成的元祖列表
# l1 = []
# for i in range(0,5,2):
#     for j in range(1,6,2):
#         l1.append((i,j))
# print(l1)
# print([(i, j) for i in range(0,5,2) for j in range(1,6,2)])

# 求M中3,6,9组成的列表M = [[1,2,3],[4,5,6],[7,8,9]]
# 求出50以内能被3整除的数的平方，并放入到一个列表中。
# 构建一个列表：['python1期', 'python2期', 'python3期', 'python4期', 'python6期', 'python7期', 'python8期', 'python9期', 'python10期']
# 构建一个列表：[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
# 构建一个列表：[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 有一个列表l1 = ['alex', 'WuSir', '老男孩', '太白']
# 将其构造成这种列表['alex0', 'WuSir1', '老男孩2', '太白3']
# l1 = ['alex', 'WuSir', '老男孩', '太白']
# print([l1[i] + str(i) for i in range(len(l1))])
# 有以下数据类型：
# x = {'name':'alex',
#      'Values':[{'timestamp':1517991992.94,'values':100,},
#                {'timestamp': 1517992000.94,'values': 200,},
#             {'timestamp': 1517992014.94,'values': 300,},
#             {'timestamp': 1517992744.94,'values': 350},
#             {'timestamp': 1517992800.94,'values': 280}],}
# 将上面的数据通过列表推导式转换成下面的类型：
# [[1517991992.94, 100], [1517992000.94, 200],
# [1517992014.94, 300], [1517992744.94, 350],
# [1517992800.94, 280]]
# print([[i['timestamp'],i['values']] for i in x['Values']])


# 用列表完成笛卡尔积
#
# 什么是笛卡尔积？ 笛卡尔积就是一个列表，列表里面的元素是由输入的可迭代类型的元素对构成的元组，因此笛卡尔积列表的长度等于输入变量的长度的乘积。

# ​	a. 构建一个列表，列表里面是三种不同尺寸的T恤衫，每个尺寸都有两个颜色（列表里面的元素为元组类型)。
# #
# colors = ['black', 'white']
# sizes = ['S', 'M', 'L']
# # [('black','s'),('black','M')]
# print([(i,j) for i in colors for j in sizes])

# ​	b. 构建一个列表,列表里面的元素是扑克牌除去大小王以后，所有的牌类（列表里面的元素为元组类型）。
#
# l1 = [('A','spades'),('A','diamonds'), ('A','clubs'), ('A','hearts')......('K','spades'),('K','diamonds'), ('K','clubs'), ('K','hearts') ]

# l1 = [i for i in range(2,11)] + list('AJQK')
# l2 = ['spades', 'diamonds', 'clubs', 'hearts']
# print([(i, j) for i in l1 for j in l2])


# 简述一下yield 与yield from的区别。
# 看下面代码，能否对其简化？说说你简化后的优点？
# def chain(*iterables):
#     # iterables ('abc', (0,1,2))
#     for it in iterables:
#         for i in it:
#             yield i

# def chain(*iterables):
#     # iterables ('abc', (0,1,2))
#     for it in iterables:
#         yield from it
#         '''
#         yield 'a'
#         yield 'b'
#         yield 'c'
#         yield 0
#         yield 1
#         yield 2
#         '''
# g = chain('abc',(0,1,2))  # ['a', 'b', 'c', 0 ,1, 2]
# print(list(g))
# print(next(g))  # a
# print(next(g))  # b
# print(next(g))  # c


# def func(x,y):
#     yield x
#     yield y
# g = func(1,2)
# print(next(g))
# print(next(g))
# print(list(g))  # 将迭代器转化成列表


# 看代码求结果（面试题）：
# v = [i % 2 for i in range(10)]
# print(v)
#
# v = (i % 2 for i in range(10))
# print(v)

# for i in range(5):
#     print(i)
# print(i)
# 看代码求结果：（面试题）
# def demo():
#     for i in range(4):
#         yield i
#
# g=demo()
#
#
# g1=(i for i in g)
# g2=(i for i in g1)
#
#
# print(list(g1))  # [i for i in g]  0 1 2 3
# # next(g1)
# # next(g1)
# # next(g1)
# # next(g1)
# print(list(g2))  # [i for i in 空的生成器]




# 看代码求结果：（面试题）

# def add(n,i):
#     return n+i
#
# def test():
#     for i in range(4):
#         yield i
#
# g=test()
#
# for n in [1,10]:
#     print(1)
#     g=(add(n,i) for i in g)
#     print(2)
# print(list(g))

# i = [1,2,3]
# i = (i+1 for i in i)
# print(i)

# day13

# func_list = []
#
# for i in range(10):
#     func_list.append(lambda :i)
# print(i)
# print(func_list)
# # def func():
# #     return i
# v1 = func_list[0]()
# v2 = func_list[5]()
# print(v1,v2)


# func_list = []
#
# for i in range(10):
#     func_list.append(lambda x: x+i)
#
# # i = 9
# print(func_list)
# # [<function <lambda> at 0x0000000001F267B8>,
# # <function <lambda> at 0x0000000001F26840>,
# # <function <lambda> at 0x0000000001F268C8>,
# # <function <lambda> at 0x0000000001F26950>, <function <lambda> at 0x0000000001F269D8>, <function <lambda> at 0x0000000001F26A60>, <function <lambda> at 0x0000000001F26AE8>, <function <lambda> at 0x0000000001F26B70>, <function <lambda> at 0x0000000001F26BF8>, <function <lambda> at 0x0000000001F26C80>]
# for i in range(0,10):
#     result = func_list[i](i)  #1  lambda 1: 1 + 1
#     print(result)  # 0 2


# def func(name):
#     v = lambda x:x+name
#     return v
#
# v1 = func('太白')  # lambda x: x + '太白'
# v2 = func('alex')  # lambda x: x + 'alex'
# v3 = v1('银角')  #
# v4 = v2('金角')
# print(v1,v2,v3,v4)

# def ip(ip_addr):
#     return int(''.join([format(int(i),'08b') for i in ip_addr.strip().split('.')]),2)

# def ip(ip_addr):
#     ip_list = ip_addr.split('.')
#     num = ''
#     for i in ip_list:
#         i = int(i)
#         num = num + format(i,'08b')
#     return int(num,2)
# ip('10.3.9.12')


# print(int('0010'))
# # 默认将一个字符串转化成十进制
#
# # 也可以将一个字符串类型的二进制转化成真正的十进制
# print(int('00000101', 2))

# format()内置函数
# print(format('test', '<20'))
# print(format('test', '>20'))
# print(format('test', '^20'))

# 将一个十进制转化成8位二进制(字符串类型)
# ret = format(10,'08b')
# print(ret,type(ret))
# l1 = [ {'sales_volumn': 0},
# 	{'sales_volumn': 108},
# 	{'sales_volumn': 337},
# 	{'sales_volumn': 475},
# 	{'sales_volumn': 396},
# 	{'sales_volumn': 172},
# 	{'sales_volumn': 9},
# 	{'sales_volumn': 58},
# 	{'sales_volumn': 272},
# 	{'sales_volumn': 456},
# 	{'sales_volumn': 440},
# 	{'sales_volumn': 239}]
# print(sorted(l1,key=lambda x:x['sales_volumn']))

# v = (lambda :x for x in range(10))
#
# print(v)
#
# # print(v[0])
# #
# # print(v[0]())
#
# print(next(v))
#
# print(next(v)())


# def num():
# # 	return [lambda x:i*x for i in range(4)]  # [lambda,lambda,lambda,lambda,]
# 	l1 = []
# 	for i in range(4):
# 		l1.append(lambda x: i*x)
# 	return l1

# print([m(2) for m in num()])


# def num():
# 	return (lambda x:i*x for i in range(4))  # 地址
# print([m(2) for m in num()])

l1 = [3,4,1,2,5,6,6,5,4,3,3]


# def sum_(alist):
	# new_list = []
	# for i in alist:
	# 	if alist.count(i) == 1:
	# 		new_list.append(i)
	# return sum(new_list)

# print(l1)

# 方法二：
# def sum_(alist):
# 	return sum([i for i in alist if alist.count(i) == 1])

# 写一个函数完成三次登陆功能：
# 用户的用户名密码从一个文件register中取出。
# register文件包含多个用户名，密码，用户名密码通过|隔开，每个人的用户名密码占用文件中一行。
# 完成三次验证，三次验证不成功则登录失败，登录失败返回False。
# 登陆成功返回True。


# def login():
#
# 	i = 0
# 	while i < 3:
# 		username = input('请输入用户名').strip()
# 		password = input('请输入密码').strip()
# 		with open('register',encoding='utf-8') as f1:
# 			for line in f1:
# 				user,pwd = line.strip().split('|')
# 				if user == username and pwd == password:
# 					return True
# 			print('用户名或者密码错误请重新输入')
# 			i += 1
# 	return False
# print(login())


# 方法二：
def get_dic_user_pwd():
	# dic = {}
	# with open('register', encoding='utf-8') as f1:
	# 		for line in f1:
	# 			user,pwd = line.strip().split('|')
	# 			dic[user] = pwd
	# return dic
	with open('register', encoding='utf-8') as f1:
		return {line.strip().split('|')[0]:line.strip().split('|')[1] for line in f1}

# print(get_dic_user_pwd())

def login(dic_upd):
	dic = dic_upd()
	i = 0
	while i < 3:
		username = input('请输入用户名').strip()
		password = input('请输入密码').strip()
		# 由于if 条件设置的不合理，致使你的代码low
		# if username not in dic:
		# 	print('用户名或者密码错误')
		# 	i += 1
		# else:
		# 	if password == dic[username]:
		# 		return True
		# 	else:
		# 		print('用户名或者密码错误')
		# 		i += 1
		if username in dic and password == dic[username]:
			return True
		i += 1
	return False
login(get_dic_user_pwd)